package com.fsnbigdata.web.deserializer;
import com.fsnbigdata.domain.ParkDistrict;
import com.fsnbigdata.service.api.ParkDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooDeserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import io.springlets.web.NotFoundException;
import org.springframework.boot.jackson.JsonComponent;

/**
 * = ParkDistrictDeserializer
 *
 * TODO Auto-generated class documentation
 *
 */
@RooDeserializer(entity = ParkDistrict.class)
@JsonComponent
public class ParkDistrictDeserializer extends JsonObjectDeserializer<ParkDistrict> {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private ParkDistrictService parkDistrictService;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private ConversionService conversionService;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param parkDistrictService
     * @param conversionService
     */
    @Autowired
    public ParkDistrictDeserializer(@Lazy ParkDistrictService parkDistrictService, ConversionService conversionService) {
        this.parkDistrictService = parkDistrictService;
        this.conversionService = conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return ParkDistrictService
     */
    public ParkDistrictService getParkDistrictService() {
        return parkDistrictService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param parkDistrictService
     */
    public void setParkDistrictService(ParkDistrictService parkDistrictService) {
        this.parkDistrictService = parkDistrictService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return ConversionService
     */
    public ConversionService getConversionService() {
        return conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conversionService
     */
    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param jsonParser
     * @param context
     * @param codec
     * @param tree
     * @return ParkDistrict
     * @throws IOException
     */
    public ParkDistrict deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) {
        String idText = tree.asText();
        Long id = conversionService.convert(idText, Long.class);
        ParkDistrict parkDistrict = parkDistrictService.findOne(id);
        if (parkDistrict == null) {
            throw new NotFoundException("ParkDistrict not found");
        }
        return parkDistrict;
    }
}
